# coding:utf8
from datetime import datetime
from flaskmovie.run import db
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    pwd = db.Column(db.String(100))
    email = db.Column(db.String(100), unique=True)
    phone = db.Column(db.String(100), unique=True)
    info = db.Column(db.String(500))
    face = db.Column(db.String(255), unique=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())
    uuid = db.Column(db.String(255), unique=True)
    userlogs = db.relationship('UserLog', backref='user')
    comments = db.relationship('Comment', backref='user')
    moviecols = db.relationship('Moviecol', backref='user')

    def __repr__(self):
        return '<User {0}>'.format(self.name)


class UserLog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    ip = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<UserLog {0}>'.format(self.id)


class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())
    movies = db.relationship('Movie', backref='tag')

    def __repr__(self):
        return '<Tag {0}>'.format(self.name)


class Movie(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    url = db.Column(db.String(255), unique=True)
    info = db.Column(db.Text)
    logo = db.Column(db.String(255), unique=True)
    star = db.Column(db.SmallInteger)
    playnum = db.Column(db.BigInteger)
    commentnum = db.Column(db.BigInteger)
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
    area = db.Column(db.String(255))
    release_time = db.Column(db.Date)
    length = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())
    comments = db.relationship('Comment', backref='movie')
    moviecols = db.relationship('Moviecol', backref='movie')

    def __repr__(self):
        return '<Moive {0}>'.format(self.title)


class Preview(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    logo = db.Column(db.String(255), unique=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<Preview {0}>'.format(self.title)


class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text)
    move_id = db.Column(db.Integer, db.ForeignKey('movie.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<Comment {0}>'.format(self.id)


class Moviecol(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    move_id = db.Column(db.Integer, db.ForeignKey('movie.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<Moviecol {0}>'.format(self.id)


class Auth(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    url = db.Column(db.String(255), unique=True)
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<Auth {0}>'.format(self.name)


class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    auths = db.Column(db.String(1024))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())
    admins = db.relationship('Admin', backref='role')

    def __repr__(self):
        return '<Role {0}>'.format(self.name)


class Admin(db.Model):  # type: db.Query
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)
    pwd = db.Column(db.String(100))
    is_super = db.Column(db.SmallInteger)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())
    adminlogs = db.relationship('AdminLog', backref='admin')
    oplogs = db.relationship('OpLog', backref='admin')

    def __repr__(self):
        return '<Admin {0}>'.format(self.name)

    def check_pwd(self, password):
        return check_password_hash(self.pwd, password)


class AdminLog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
    ip = db.Column(db.String(100))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<AdminLog {0}>'.format(self.id)


class OpLog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
    ip = db.Column(db.String(100))
    reason = db.Column(db.String(512))
    create_time = db.Column(db.DateTime, index=True, default=datetime.now())

    def __repr__(self):
        return '<Oplog {0}>'.format(self.id)
